<?php


namespace app\general\controller;

use app\v2\model\SqArticleSale;
use phpspider\core\requests;
use phpspider\core\selector;
use think\Controller;

/**
 * Class Sales
 * @package app\general\controller
 * 销售秘籍爬取网站
 */
class Sales extends Controller
{

   /**
    * 爬取物友网
    * @url http://news.56ye.net/
    */
   public function clicb(){
       $category=['销售技巧','网络营销','做生意','谈生意','电话销售','创业故事'];
       $category_id=[45720,432,45714,45713,45715,430];
       foreach ($category_id as $k=>$id){
           $where=array(
               "genre"=>$id
           );
           $pn=(new SqArticleSale())->where($where)->order("id desc")->limit(1)->value("pn");
           $pn=($pn)?$pn+1:1;
           $url="http://news.56ye.net/list-".$id."-$pn.html";
           requests::$input_encoding = 'utf-8';
           requests::$output_encoding = 'utf-8';
           requests::set_useragent(array(
               "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3902.4 Safari/537.36"
           ));
           $html=requests::get($url);
           //获取标题
           $selector = ".//div[@class='PostHead']/span/h3/a/text()";
           $title = selector::select($html, $selector,'xpath');
           //获取地址
           $selector = ".//div[@class='PostHead']/span/h3/a/@href";
           $url = selector::select($html, $selector,'xpath');
           if($title==null){
               $id=$this->recordTimers(10);
               $message=$category[$k]."暂无文章";
               $this->updateTimerType($id,$message);
           }else{
               $pn=$pn+1;
               $result=$this->getContent($title,$url,$id,$pn);
               $id=$this->recordTimers(10);
               $message=$result?$category[$k]."爬取文章成功":"爬取文章失败";
               $this->updateTimerType($id,$message);
           }

       }
       echo "爬取完成";
   }

    /**
     * 获取内容
     * @param $title
     * @param $url
     * @param $genre
     * @param $pn
     * @return bool
     */
   public function getContent($title,$url,$genre,$pn){
       requests::$input_encoding = 'utf-8';
       requests::$output_encoding = 'utf-8';
       $fromData=array();
       foreach ($url as $k=>$u){
           $html=requests::get($u);
           //获取内容
           $selector="//div[contains(@id,'content')]";
           $content = selector::select($html, $selector,'xpath');
           //获取来源
           $selector=".//div[@class='info pubDate']/a/text()";
           $source = selector::select($html, $selector,'xpath');
           //获取来源
           $selector=".//div[@class='info pubDate']/text()";
           $author = selector::select($html, $selector,'xpath');
           $author=$author[1];
           $author=mb_substr($author,5,3);
           $data=array(
               "title"=>$title[$k],
               "source"=>$source,
               "author"=>$author,
               "content"=>$content,
               "create_time"=>time(),
               "genre"=>$genre,
               "pn"=>$pn
           );
           $fromData[]=$data;
       }
       $flag=(new SqArticleSale())->insertAll($fromData);
       return ($flag)?true:false;
   }
    //记录定时器调用时间
    public function recordTimers($type)
    {
        $data = [
            'type' => $type,
            'create_time' => time(),
            'status' => 0
        ];
        return db('sq_record_timer')->insertGetId($data);
    }

    public function updateTimerType($id,$remark='')
    {
        db('sq_record_timer')->where('id', $id)->update(['status'=>1, 'remark'=>$remark]);
    }
}